package by.dkusch.aircompany.webapp.page.base.common;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

import by.dkusch.aircompany.model.tables.UserGrant;
import by.dkusch.aircompany.model.tables.base.BaseEntity;
import by.dkusch.aircompany.webapp.app.BasicAuthenticationSession;
import by.dkusch.aircompany.webapp.page.base.BaseDetailLayout;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;

public class GrantCommonPanel<ID, Entity extends BaseEntity<ID>, P extends BaseLayout> extends Panel {

	private static final long serialVersionUID = 1L;

	public GrantCommonPanel(String id, final Entity entity, final Class<P> page) {
		super(id);
		add(new Link<Void>("edit-grants") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new BaseDetailLayout<ID, Entity>(entity, ActionType.EDIT, new PageCreator() {

					private static final long serialVersionUID = 1L;

					@Override
					public Page createPage() {
						try {
							return page.newInstance();
						} catch (InstantiationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						return null;
					}
				}, UserGrant.class));

			}
		});
		setVisible(BasicAuthenticationSession.get().isUserDba());

	};

}
